﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Asteroids
{
    public class AnimatedSprite
    {
        Texture2D m_texture;
        SpriteEffects m_effects;

        float m_frameRate = 0.02f;
        float m_elapsed = 0.0f;

        int m_frameOffsetX = 0;
        int m_frameOffsetY = 0;
        int m_frameWidth = 32;
        int m_frameHeight = 32;

        int m_frameCount = 1;
        int m_currentFrame = 0;
        int m_screenX = 0;
        int m_screenY = 0;

        bool m_isAnimating = true;

        public int frameOffsetY
        {
            get { return m_frameOffsetY; }
            set { m_frameOffsetY = value; }
        }

        public int currentFrame
        {
            get { return m_currentFrame; }
        }

        public int frameCount
        {
            get { return m_frameCount; }
        }

        public int width
        {
            get { return m_frameWidth; }
        }

        public AnimatedSprite(Texture2D texture, int frameOffsetX, int frameOffsetY, int frameWidth, int frameHeight, int frameCount)
        {
            m_texture = texture;
            m_frameOffsetX = frameOffsetX;
            m_frameOffsetY = frameOffsetY;
            m_frameWidth = frameWidth;
            m_frameHeight = frameHeight;
            m_frameCount = frameCount;
        }

        public void Update(GameTime gametime)
        {
            if (m_isAnimating)
            {
                // Accumulate elapsed time...
                m_elapsed += (float)gametime.ElapsedGameTime.TotalSeconds;

                // Until it passes our frame length
                if (m_elapsed > m_frameRate)
                {
                    // Increment the current frame, wrapping back to 0 at iFrameCount
                    m_currentFrame = (m_currentFrame + 1) % m_frameCount;

                    // Reset the elapsed frame time.
                    m_elapsed = 0.0f;
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch, Vector2 position, Vector2 center, float rotation)
        {
            //Hey GUYS. Origin should actually be item.centerPosition-item.position, but that's just a lesson learned thing. Don't try to change it.
            spriteBatch.Draw(m_texture, new Rectangle(m_screenX + (int)center.X, m_screenY + (int)center.Y, m_frameWidth, m_frameHeight),
                                new Rectangle(m_frameOffsetX + (m_frameWidth * m_currentFrame), m_frameOffsetY, m_frameWidth, m_frameHeight),
                                Color.White, rotation, center - position, m_effects,0);
        }

        public void Draw(SpriteBatch spriteBatch, Vector2 position, Vector2 center, float rotation, Color color)
        {
            spriteBatch.Draw(m_texture, new Rectangle(m_screenX + (int)center.X, m_screenY + (int)center.Y, m_frameWidth, m_frameHeight),
                                new Rectangle(m_frameOffsetX + (m_frameWidth * m_currentFrame), m_frameOffsetY, m_frameWidth, m_frameHeight),
                                color, rotation, center - position, m_effects, 0);
        }
    }
}
